package com.example.springbootjdbc.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import javax.sql.DataSource;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

/**
 * @ClassName JdbcConfiguration
 * @Description JdbcConfiguration
 * @Author ZX
 * @Date 2020/5/26
 */
//@Configuration
//@EnableConfigurationProperties(JdbcProperties.class)//指定加载配置信息属性的类
//@PropertySource("classpath:/jdbc.properties") //指定加载properties配置文件 法1
public class JdbcConfiguration {

    //@Autowired  //1. 自动注入JdbcProperties
//    private JdbcProperties jdbcProperties;
//
//    //2. 通过构造方法注入JdbcProperties
//    public JdbcConfiguration(JdbcProperties jdbcProperties){
//        this.jdbcProperties = jdbcProperties;
//    }

//    @Value("${jdbc.driverClassName}")  //指定加载properties配置文件中的参数
//    private  String driverClassName;
//    @Value("${jdbc.username}")
//    private  String username;
//    @Value("${jdbc.password}")
//    private  String password;
//    @Value("${jdbc.url}")
//    private  String url;

    /**
     * @Description: 实例化Druid 最简便，不用jdbcProperties配置信息属性的类
     * @Param:
     * @return:
     */
    @Bean
    //名字相同自动注入
    @ConfigurationProperties(prefix = "jdbc") //名字相同自动注入
    public DataSource geDataSource(){
        DruidDataSource source = new DruidDataSource();
        return source;
    }

//    @Bean
//    public DataSource geDataSource(){
//        DruidDataSource source = new DruidDataSource();
//        source.setPassword(this.jdbcProperties.getPassword());
//        source.setUsername(this.jdbcProperties.getUsername());
//        source.setUrl(this.jdbcProperties.getUrl());
//        source.setDriverClassName(this.jdbcProperties.getDriverClassName());
//        return source;
//    }


//    @Bean    //3. 参数注入
//    public DataSource geDataSource(JdbcProperties jdbcProperties){
//        DruidDataSource source = new DruidDataSource();
//        source.setPassword(jdbcProperties.getPassword());
//        source.setUsername(jdbcProperties.getUsername());
//        source.setUrl(jdbcProperties.getUrl());
//        source.setDriverClassName(jdbcProperties.getDriverClassName());
//        return source;
//    }



}
